using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Perovich.GameObjects.TwoDimensional
{

    /// <summary>
    /// Represents a style of movement when hitting a border.
    /// </summary>
    public enum BorderStyle
    {
        /// <summary>
        /// The <see cref="BoundedObject2D"/> will wrap to the other side of the border.
        /// </summary>
        Wrap,
        /// <summary>
        /// The <see cref="BoundedObject2D"/> will bounce off the edges of the border.
        /// </summary>
        Bounce
    }

    /// <summary>
    /// The <see cref="BoundedObject2D"/> class is an <see cref=" InteractiveObject2D"/> that is restricted to a certain boundary.
    /// </summary>
    public class BoundedObject2D : InteractiveObject2D
    {
        /// <summary>
        /// Gets or sets the bounds for this object.
        /// </summary>
        public BoundingBox Bounds
        {
            get
            {
                return (BoundingBox)CollisionTargets[0];
            }
            set
            {
                CollisionTargetsMutable[0] = value;
            }
        }


        /// <summary>
        /// Gets or sets the <see cref="BorderStyle"/> of this object when it goes out of bounds.
        /// </summary>
        public BorderStyle MovementStyle
        {
            get;
            set;
        }


        /// <summary>
        /// Constructs a new instance of the <see cref="BoundedObject2D"/> class.
        /// </summary>
        /// <param name="game"><see cref="Perovich.GameObjects.Interfaces.ICanHoldComponents"/> instance that this <see cref="DrawableComponent"/> will be constructed for.</param>
        public BoundedObject2D(Interfaces.ICanHoldComponents game)
            : base(game)
        {
            MovementStyle = BorderStyle.Bounce;
            AddCollisionTargets(new BoundingBox());
            Collided += new EventHandler<CollisionEventArgs>(BoundedObject2D_Collided);
        }

        void BoundedObject2D_Collided(object sender, CollisionEventArgs e)
        {
            if (e.Other is BoundingBox)
            {
                if (MovementStyle == BorderStyle.Bounce)
                {
                    DoBounce();
                }
                else if (MovementStyle == BorderStyle.Wrap)
                {
                    DoWrap();
                }
            }
        }

        private void DoWrap()
        {
            Vector3 size = Bounds.Max - Bounds.Min;

            // Assuming length is along x axis, width is along z, and height is along y.  Your requirements may vary. 
            float width = size.X;
            float height = size.Y;

            float MaxX =
                 Bounds.Min.X + width + (int)Origin.X;
            float MinX = Bounds.Min.X - Texture.Width + (int)Origin.X;
            float MaxY =
                Bounds.Min.Y + height + (int)Origin.Y;
            float MinY = Bounds.Min.Y - Texture.Height + (int)Origin.Y;

            // Check for wrap.
            if (Position.X > MaxX)
            {
                Position = new Vector2(MinX + Position.X - MaxX, Position.Y);
            }

            else if (Position.X < MinX)
            {
                Position = new Vector2(MaxX + Position.X - MinX, Position.Y);
            }

            if (Position.Y > MaxY)
            {
                Position = new Vector2(Position.X, MinY + Position.Y - MaxY);
            }

            else if (Position.Y < MinY)
            {
                Position = new Vector2(Position.X, MaxY + Position.Y - MinY);
            }
        }

        private void DoBounce()
        {
            Vector3 size = Bounds.Max - Bounds.Min;

            // Assuming length is along x axis, width is along z, and height is along y.  Your requirements may vary. 
            float width = size.X;
            float height = size.Y;

            float MaxX =
                 Bounds.Min.X + width - Texture.Width + (int)Origin.X;
            float MinX = Bounds.Min.X + (int)Origin.X;
            float MaxY =
                Bounds.Min.Y + height - Texture.Height + (int)Origin.Y;
            float MinY = Bounds.Min.Y + (int)Origin.Y;

            // Check for bounce.
            if ((Position.X > MaxX) && (Velocity.X > 0))
            {
                Velocity = new Vector2(-1 * Velocity.X, Velocity.Y);
            }

            else if ((Position.X < MinX) && (Velocity.X < 0))
            {
                Velocity = new Vector2(-1 * Velocity.X, Velocity.Y);
            }

            if ((Position.Y > MaxY) && (Velocity.Y > 0))
            {
                Velocity = new Vector2(Velocity.X, -1 * Velocity.Y);
            }

            else if ((Position.Y < MinY) && (Velocity.Y < 0))
            {
                Velocity = new Vector2(Velocity.X, -1 * Velocity.Y);
            }
        }

    }
}
